'use strict';

import HTTPUtil from './HTTPUtil'
import Global from './GlobalConst';
export default class APIService {
    //API接口url	正式：chb2c.xiarikui.cn 测试：conghuab2c.xiarikui.cn
    static HOST = 'http://chb2c.xiarikui.cn/api/';

    /**用户登录
     * @param  username{[手机号]}
     * @param  userpwd{[密码]}
     */
    static userlogin(username, userpwd) {
        var url = this.HOST + 'dis_login.php';
        let formData = new FormData();
        formData.append("seller_name", username);
        formData.append("password", userpwd);
        return new Promise(function (resolve, reject) {
            HTTPUtil.post(url, formData).then((json)=> {
                // console.log(json.result);
                if (json.code === 200) {
                    resolve(json.result);
                }
                else if (json.code === 100) {
                    reject(new Error('登录失败'));
                }
                else {
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                // console.log(err);
                reject(new Error(err));
            });
        });
    }

    /**提取保存在本地的用户登录信息
     * @return {[成功，返回username,password,result]}
     */
    static loadStorageUserInfo() {
        return new Promise(function (resolve, reject) {
            storage.load({
                key: Global.Const.storageUser,
            }).then((ret) => {
                resolve(ret);
            }).catch((err) => {
                reject(new Error(err));
            });
        });
    }

    /**提取保存在本地的用户密钥或直接返回输入的密钥
     * @param  input{[可为null]}
     * @return {[成功，返回密钥]}
     */
    static getUserKey(input) {
        if (input === null) {
            return new Promise(function (resolve, reject) {
                APIService.loadStorageUserInfo().then((ret)=> {
                    // console.log('succ');
                    // console.log(ret.result.user_key);
                    resolve(ret.result.user_key);
                }).catch((err)=> {
                    // console.log('fail');
                    // console.log(err);
                    reject(new Error('获取用户密钥失败'));
                });
            });
        }
        else {
            return new Promise(function (resolve, reject) {
                resolve(input);
            });
        }
    }

    /**session过期重新登录
     * @return {[成功，返回密钥，并更新本地用户登录信息数据]}
     */
    static reloginWhenSessionExpire() {
        return new Promise(function (resolve, reject) {
            APIService.loadStorageUserInfo().then((ret)=> {
                APIService.userlogin(ret.username, ret.password).then((result)=> {
                    storage.save({
                        key: Global.Const.storagekeyLogin,
                        rawData: {
                            username: ret.username,
                            password: ret.password,
                            result: result,
                        }
                    }).then(()=> {
                        // console.log('save new key=='+result.user_key);
                        resolve(result.user_key);
                    });
                }).catch((err)=> {
                    reject(new Error('获取用户信息失败'));
                });
            }).catch((err)=> {
                reject(new Error('获取用户信息失败'));
            });
        });
    }
}